package com.ggy.babybed.network;

import android.app.Service;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.ggy.babybed.socketwork.util.NetworkUtil;
import com.ggy.babybed.wilddog.WildDogDataChange;

/**
 * Created by sizi on 2016/5/17.
 */
public final class NetWorkManager {

    public static final String TAG = "NetWorkManager";

    public static final int USING_WILDOG = 1;
    public static final int USING_UDP = 2;

    private Context mContext;
    private Object mLock = new Object();
    private INetWorkService mNetWorkService;
    private static NetWorkManager sInstance;

    private NetWorkManager(Context context) {
        this.mContext = context;
        getNetWorkServiceDefault(context);
    }

    public static NetWorkManager getNetWorkManager(Context context) {
        if (sInstance == null) {
            synchronized (NetWorkManager.class) {
                if (sInstance == null) {
                    sInstance = new NetWorkManager(context);
                }
            }
        }
        return sInstance;
    }

    public boolean setNetWorkServiceByType(int type) {
        if (type != USING_WILDOG || type != USING_UDP) {
            Log.e(TAG, "not support networkType!");
            return false;
        }
        return true;
    }

    public INetWorkService getNetWorkServiceDefault(Context context) {
        if (mNetWorkService == null) {
            mNetWorkService = new WilddogService(context);
        }
        return mNetWorkService;
    }


    public boolean steLedStatus(boolean status) {
        if (!isNetworkAvailable()) {
            Log.e(TAG, "net work is not available!");
            return false;
        }
        synchronized (mLock) {
            mNetWorkService.setLed(status);
        }
        return true;
    }

    public boolean startListenNode(Service need, String node) {
        if(need ==null){
            Log.e(TAG,"you need service to start !");
            return false;
        }
        return mNetWorkService.startNodeListen(node);
    }

    public boolean addListenForNode(String node, WildDogDataChange dc) {
        if (!isNetworkAvailable()) {
            Log.e(TAG, "net work is not available!");
            return false;
        }
        return mNetWorkService.addNodeLisen(node, dc);
    }

    public void rmListenForNode(String node, WildDogDataChange dc) {
        mNetWorkService.rmNodeListen(node, dc);
    }

    public boolean isNetworkAvailable() {
        return NetworkUtil.isConnected(mContext);
    }

}
